package com.itheima.web.controller.oms;

import com.itheima.common.exception.CustomException;
import com.itheima.dto.oms.OrderDeliveryDto;
import com.itheima.dto.oms.OrderMoneyDto;

import com.itheima.dto.oms.OrderReceiverDto;
import com.itheima.result.AjaxResult;
import com.itheima.result.PageResult;
import com.itheima.service.oms.OmsOrderService;
import com.itheima.vo.oms.OmsOrder;
import com.itheima.vo.oms.OrderDetailsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OmsOrderController {

    @DubboReference
    private OmsOrderService omsOrderService;

    /**
     * 分页查询订单详情
     *
     */
    @GetMapping("/list")
    public AjaxResult list(String createTime,String orderSn,String receiverKeyword,Integer orderType,
                           Integer pageNum,Integer pageSize,Integer sourceType,Integer status) {
        PageResult vo = omsOrderService.listPage(createTime,orderSn,receiverKeyword,orderType,pageNum,pageSize,sourceType,status);
        return AjaxResult.success(vo);
    }

    /**
     * 修改订单费用信息
     */
    @PostMapping("/update/moneyInfo")
    public AjaxResult updateMoneyInfo(@RequestBody OrderMoneyDto dto) {
        OmsOrder omsOrder = new OmsOrder();
        BeanUtils.copyProperties(dto,omsOrder);
        omsOrderService.updateById(omsOrder);
        omsOrderService.note(omsOrder.getId(),0,"修改订单费用信息");

        return AjaxResult.success("修改成功!");
    }

    /**
     * 修改收货人信息
     */
    @PostMapping("/update/receiverInfo")
    public AjaxResult updateReceiverInfo(@RequestBody OrderReceiverDto dto) {
        omsOrderService.updateReceiver(dto);
        return AjaxResult.success("修改成功!");
    }

    /**
     * 批量删除订单
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestParam List<Integer> ids) {
        Boolean count = omsOrderService.removeByIds(ids);
        if (count) {
            return AjaxResult.success("删除成功!");
        }
        return AjaxResult.error("关闭订单失败!");
    }

    /**
     * 批量关闭订单
     */
    @PostMapping("/update/close")
    public AjaxResult close(@RequestParam List<Integer> ids,@RequestParam String note) {
        omsOrderService.closeOrders(ids,note);
        return AjaxResult.success("关闭订单成功!");
    }

    /**
     * 批量发货
     */
    @PostMapping("/update/delivery")
    public AjaxResult delivery(@RequestBody List<OrderDeliveryDto> list) {
        omsOrderService.delivery(list);
        return AjaxResult.success("发货成功!");
    }

    /**
     * 根据订单id获取订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult findOrderDetails(@PathVariable Integer id) {
        OrderDetailsVo vo =  omsOrderService.findById(id);
        return AjaxResult.success(vo);
    }

    /**
     * 备注订单
     */
    @PostMapping("/update/note")
    public AjaxResult note(Long id,Integer status,String note) {
        Integer count = omsOrderService.note(id,status,note);
        if (count == 0) {
            return AjaxResult.error("备注失败!");
        }
        return AjaxResult.success("备注成功!");
    }
}